package ua.pp.bizon.yandex.api.impl;

import ua.pp.bizon.yandex.api.IAppService;
import ua.pp.bizon.yandex.api.IJobExecutor;
import ua.pp.bizon.yandex.api.JobInfo;
import ua.pp.bizon.yandex.api.ObjectLookup;
import ua.pp.bizon.yandex.nb.PhotosNbModel;
import ua.pp.bizon.yandex.uploader.atom.AlbumsList;
import ua.pp.bizon.yandex.uploader.atom.EntryAlbum;

import java.io.File;
import java.util.Arrays;
import java.util.TreeMap;

import org.apache.commons.logging.LogFactory;

final class UploadFoldersJob implements JobInfo {
	private String from;
	private String to;
	private IJobExecutor executor;

	UploadFoldersJob(String from, String to, IJobExecutor executor) {
		this.from = from;
		this.to = to;
		this.executor = executor;
		LogFactory.getLog(getClass()).trace("new " + toString());
	}

	@Override
	public String toString() {
		return "UploadFoldersJob [from=" + from + ", to=" + to + ", executor=" + executor + "]";
	}

	@Override
	public void run() {
		File folderFrom = new File(from);
		AlbumsList list = ObjectLookup.lookup(IAppService.class).get().getAlbumsList().findFolderByID(to);
		EntryAlbum to = list.createAlbum(folderFrom.getName());
		executor.exec(new UploadFilesJob(Arrays.asList(folderFrom.listFiles(new PhotosNbModel.JpgFilter())), to, executor));
	}

	@Override
	public Element getState() {
		Element element = new Element();
		element.setActivity(Activity.UPLOAD);
		element.setProperties(new TreeMap<String, String>());
		element.getProperties().put(Element.PROP_FROM, from);
		element.getProperties().put(Element.PROP_TO, to);
		element.getProperties().put(Element.PROP_COUNT_DONE, "0");
		element.getProperties().put(Element.PROP_COUNT_TOTAL, "1");
		return element;
	}
}